//
//  ListViewController.swift
//  RXmvvmDemo
//
//  Created by jemi on 2019/12/27.
//  Copyright © 2019 jemi. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import SnapKit

class ListViewController: UIViewController {
    var tableView = UITableView()
    let bag = DisposeBag()
    var obViewModel:OBViewModel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //创建表格视图
//        self.tableView = UITableView(frame: self.view.frame, style:.plain)
        //创建一个重用的单元格
        tableView.register(UITableViewCell.self,
                                forCellReuseIdentifier: "Cell")
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (m) in
            m.edges.equalToSuperview()
        }
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_footer = MJRefreshBackNormalFooter()
        
//        let listViewModel = ListViewModel(headRefresh: (self.tableView.mj_header?.rx.refreshing.asDriver())!)
//
//    listViewModel.tableData.asDriver().drive(self.tableView.rx.items) { (tableView, row, element) in
//            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
//            cell.textLabel?.text = "\(row+1)、\(element)"
//            return cell
//        }.disposed(by: bag)
            
        
//    listViewModel.endRefreshing.drive((self.tableView.mj_header?.rx.endRefreshing)!).disposed(by: bag)
        
        let obViewModel = OBViewModel(input: (
            headerRefresh: self.tableView.mj_header!.rx.refreshing.asObservable(),
            footerRefresh: self.tableView.mj_footer!.rx.refreshing.asObservable()),
            dependency: (disposeBag: self.bag, networkService: NetWorkService()))
        self.obViewModel = obViewModel
        
        obViewModel.tableData.bind(to: tableView.rx.items) {
            (tableView, row , element) in
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
            cell.textLabel?.text = "\(row+1)、\(element)"
            cell.textLabel?.textColor = .red
            return cell
        }.disposed(by: bag)
        
        obViewModel.endHeadRefreshing.bind(to: self.tableView.mj_header!.rx.endRefreshing).disposed(by: bag)

        obViewModel.endFootRefreshing.bind(to: self.tableView.mj_footer!.rx.endRefreshing).disposed(by: bag)
    }
    
    @objc func injected(){
        print("any sss \(self)")
        print("1111222222")
        self.navigationItem.title = "111v11222ssss2222"
    }
}
